package com.cskaoyan.service.goods;

import com.cskaoyan.mapper.*;
import com.cskaoyan.model.bean.*;
import com.cskaoyan.model.bean.Brand;
import com.cskaoyan.model.bo.goods.*;
import com.cskaoyan.model.vo.goods.*;
import com.cskaoyan.model.vo.goods.CatVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public  class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    BrandMapper brandMapper;


//    @Autowired
//    BrandMapper brandMapper;
//
//    @Autowired
//    CategoryMapper categoryMapper;

    @Override
    public GoodsVo list(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setOrderByClause(sort + " " + order);
        List<Goods> goods = goodsMapper.selectByExampleWithBLOBs(goodsExample);

        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
        long total = goodsPageInfo.getTotal();

        GoodsVo goodsVo = new GoodsVo();
        goodsVo.setItems(goods);
        goodsVo.setTotal(total);
        return goodsVo;

    }

    @Override
    public GoodsVo goodsSearch(Integer page, Integer limit, String goodsSn, String name, String sort, String order) {
        PageHelper.startPage(page, limit);

        GoodsExample goodsExample = new GoodsExample();
        goodsExample.setOrderByClause(sort + " " + order);   //${}做Sql拼接
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (goodsSn != null && !"".equals(goodsSn)) {
            criteria.andGoodsSnEqualTo(goodsSn);
        }

        List<Goods> goods = goodsMapper.selectByExampleWithBLOBs(goodsExample);

        PageInfo<Goods> goodsPageInfo = new PageInfo<>(goods);
        long total = goodsPageInfo.getTotal();

        GoodsVo goodsVo = new GoodsVo();
        goodsVo.setItems(goods);
        goodsVo.setTotal(total);
        return goodsVo;

    }

    @Override
    public CatAndBrandVo catAndBrandSelect() {

        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        List<CatVo> catVoList = new ArrayList<>();
        for (Category category :categories) {
            //
            CatVo catVo = new CatVo();
            Integer id = category.getId();
            catVo.setValue(id);     //1
            catVo.setLabel(category.getName());//2

            CategoryExample categoryExampleChir = new CategoryExample();
            CategoryExample.Criteria criteriaChir = categoryExampleChir.createCriteria();
            criteriaChir.andPidEqualTo(id);

            List<Category> childs = categoryMapper.selectByExample(categoryExampleChir);

            List<ChildrenCat> childrenCats = new ArrayList<>();
            for (Category child: childs) {
                childrenCats.add(new ChildrenCat(child.getId(), child.getName()));
            }

            catVo.setChildren(childrenCats);
            catVoList.add(catVo);

        }

        BrandExample brandExample = new BrandExample();
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        ArrayList<BrandVo> brandVos = new ArrayList<>();
        for (Brand brand: brands) {
            BrandVo brandVo = new BrandVo();
            brandVo.setValue(brand.getId());
            brandVo.setLabel(brand.getName());
            brandVos.add(brandVo);
        }

        CatAndBrandVo catAndBrandVo = new CatAndBrandVo();
        catAndBrandVo.setCategoryList(catVoList);
        catAndBrandVo.setBrandList(brandVos);

        return catAndBrandVo;
    }

    @Override
    public void goodsAdd(GoodsAddBo goodsAddBo) {

        Goods goods = goodsAddBo.getGoods();
        List<Specification> specifications = goodsAddBo.getSpecifications();
        List<Product> products = goodsAddBo.getProducts();
        List<Attribute> attributes = goodsAddBo.getAttributes();


        goodsMapper.insertSelective(goods);
        //批量操作
        goodsSpecificationMapper.insertGoodsSpecifications(specifications);
        goodsProductMapper.insertGoodsProduct(products);
        goodsAttributeMapper.insertGoodsAttribute(attributes);



    }

    @Override
    public DetailVo detail(Integer id) {
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        Integer categoryId = goods.getCategoryId();//2
        Integer catId = categoryMapper.selectPIdById(categoryId);//1

        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria criteria = goodsAttributeExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);

        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria Specriteria = goodsSpecificationExample.createCriteria();
        Specriteria.andGoodsIdEqualTo(id);
        List<GoodsSpecification> specifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);

        GoodsProductExample goodsProductExample = new GoodsProductExample();
        GoodsProductExample.Criteria Procriteria= goodsProductExample.createCriteria();
        Procriteria.andGoodsIdEqualTo(id);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);

        DetailVo detailVo = new DetailVo();
        detailVo.setGoods(goods);
        detailVo.setCategoryIds(new int[]{catId,categoryId});
        detailVo.setAttributes(goodsAttributes);
        detailVo.setSpecifications(specifications);
        detailVo.setProducts(goodsProducts);
//

        return detailVo;
    }

    @Override
    public void goodsUpdate(GoodsUpdateBo goodsUpdateBo) {
        Goods goods = goodsUpdateBo.getGoods();
        Integer goodsId = goods.getId();
        List<GoodsSpecification> specifications = goodsUpdateBo.getSpecifications();
        List<GoodsProduct> products = goodsUpdateBo.getProducts();
        List<GoodsAttribute> attributes = goodsUpdateBo.getAttributes();

        int goodsRet = goodsMapper.updateByPrimaryKeyWithBLOBs(goods);

        for (GoodsSpecification specification: specifications) {
            goodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }

        for (GoodsProduct product :products) {
            goodsProductMapper.updateByPrimaryKeySelective(product);
        }

        for (GoodsAttribute attribute : attributes) {
            goodsAttributeMapper.updateByPrimaryKeySelective(attribute);
        }



    }

    @Override
    public int goodsDelete(Goods goods) {
        Goods Delgoods = new Goods();
        Delgoods.setId(goods.getId());
        Delgoods.setDeleted(true);
        int ret=0;
        if (goods.getDeleted() == false) {
            ret = goodsMapper.updateByPrimaryKeySelective(Delgoods);
        }

        return ret;
    }

    @Override
    public CommentVo commentList(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        CommentExample commentExample = new CommentExample();
        commentExample.setOrderByClause(sort + " " + order);
        List<Comment> comments = commentMapper.selectByExample(commentExample);

        PageInfo<Comment> commentPageInfo = new PageInfo<>(comments);
        long total = commentPageInfo.getTotal();

        CommentVo commentVo = new CommentVo();
        commentVo.setItems(comments);
        commentVo.setTotal(total);
        return commentVo;


    }

    @Override
    public int commentDel(Comment comment) {
        Comment delComment = new Comment();
        delComment.setId(comment.getId());
        delComment.setDeleted(true);
        int ret = 0;
        if (comment.getDeleted() == false) {
            ret = commentMapper.updateByPrimaryKeySelective(delComment);
            return ret;
        }
        return 0;
    }
       /* Goods goods = goodsUpdateBo.getGoods();
        //TODO    call goodsMapper
        int goodsRet = goodsMapper.insert(goods);
        List<GoodsSpecification> specifications = goodsUpdateBo.getSpecifications();
        //TODO call goodsSpecificationMapper
        int speRet = goodsSpecificationMapper.insertGoodsUpdateSpecifications(specifications);
        List<GoodsProduct> products = goodsUpdateBo.getProducts();
        //TODO call goodsProductMapper 批量操作
        int prodRet = goodsProductMapper.insertGoodsUpdateProduct(products);
        List<GoodsAttribute> attributes = goodsUpdateBo.getAttributes();
        //TODO call 批插入
        int attRet = goodsAttributeMapper.insertGoodsUpdateAttribute(attributes);
        if ((goodsRet + speRet + prodRet + attRet) == 0) {
            return 400;
        }
        return 200;
    }*/

//    @Override
//    public StorageVO goodsAdd(Storage storage) {
//
//        storageMapper.insertSelective(storage);
//        StorageVO storageVO = goodsMapper.storageSelect(storage.getUrl());
//        return storageVO;
//
//    }




}
